//
//  WPReadingVideoOrAudioPlayerVC.m
//  wobingwoyi
//
//  Created by WGP on 2016/12/5.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPReadingVideoOrAudioPlayerVC.h"
#import "WPVideoOrAudioPlayCell.h"
#import "WPPlayController.h"
#import "WPAudioPlayController.h"
#import "WPVideoOrAudioModel.h"
#import "WPReadingAudioPlayerVC.h"

@interface WPReadingVideoOrAudioPlayerVC ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic,strong)UICollectionView *collectionView;

@end

@implementation WPReadingVideoOrAudioPlayerVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
     [self collectionView];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.selectedIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
}

-(UICollectionView *)collectionView
{
    if (_collectionView == nil)
    {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.pagingEnabled = YES;
        _collectionView.bounces = NO;
        CGFloat itemW = self.view.bounds.size.width;
        CGFloat itemH = self.view.bounds.size.height;
        layout.itemSize = CGSizeMake(itemW, itemH);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        [_collectionView registerClass:[WPVideoOrAudioPlayCell class] forCellWithReuseIdentifier:@"WPVideoOrAudioPlayCell"];
        [self.view addSubview:_collectionView];
        
    }
    return _collectionView;
}

#pragma mark - collectionView的数据源和代理方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.videoAndAudioArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WPVideoOrAudioPlayCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WPVideoOrAudioPlayCell" forIndexPath:indexPath];
    //    移除上一个控制器
    [cell.vc.view removeFromSuperview];
    
    WPVideoOrAudioModel *model = self.videoAndAudioArr[indexPath.item];
    
    if ([model.type isEqualToString:@"video"])
    {
        WPPlayController *vc = [WPPlayController new];
        vc.index = indexPath.item;
        vc.url = [NSURL URLWithString:model.fileUrl];
        //    传递控制器
        cell.vc = vc;
        vc.isReading = YES;
        vc.backBlock = ^{
            
            [self dismissViewControllerAnimated:YES completion:nil];
        };
        
        vc.deleteBlock = ^(NSInteger index){
            
        };
        
        //如果一个控制器的View作为另一个控制器的子控件那么这个控制器也应该作为另一个控制器的子控制器
        [self addChildViewController:vc];
    }
    else
    {
        WPReadingAudioPlayerVC *vc = [WPReadingAudioPlayerVC new];
        vc.index = indexPath.item;
        vc.url = [NSURL URLWithString:model.fileUrl];
        //    传递控制器
        cell.vc = vc;
        vc.backBlock = ^{
            
            [self dismissViewControllerAnimated:YES completion:nil];
        };
        
        //如果一个控制器的View作为另一个控制器的子控件那么这个控制器也应该作为另一个控制器的子控制器
        [self addChildViewController:vc];
    }
    
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
